import axios from 'axios'
import {
    h,
} from 'vue'
import app from  '@/app'
import {message, notification} from 'ant-design-vue';
import {jump, setObjectValue} from "@/utils/index";
import ajax from "@/utils/ajax";
import router from '@/router'
import {action,state} from '@/store/index'
import token from '@/store/token'
import render from '@/components/render/render.vue'
import {forEach} from 'lodash'
const service = axios.create({
    // baseURL: window.global_config.VUE_APP_BASE_API, // url = base url + request url

    baseURL: import.meta.env.VITE_APP_BASE_API, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 0 // request timeout
})
// request interceptor
service.interceptors.request.use(
    (config) => {
        // do something before request is sent
        // let each request carry token
        // ['X-Token'] is a custom headers key
        // please modify it according to the actual situation
        config.headers['App-Name'] = window['App-Name']
        config.headers['X-Requested-With'] = 'XMLHttpRequest'
        config.headers['Authorization'] = token.get()
        return config
    },
    (error) => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
    }
)
service.interceptors.response.use(
    (  /**
        * If you want to get http information such as headers or status
        * Please return  response => response
        */

       /**
        * Determine the request status by custom code
        * Here is just an example
        * You can also judge the status by HTTP Status Code
        */
        response) => {

        const res = response.data

        // if the custom code is not 20000, it is judged as an error.
        if (res.code === 200) {
            return res
        }else if(res.code === 70000){
            return ajax(res.data)
        } else if(res.code === 80010){

            notification.open(responseBefore(res.data))
            return responseHandel(res.data)
        } else if(res.code === 80020){
            message.open(responseBefore(res.data))
            return responseHandel(res.data)
        } else {
            if (typeof (res) == 'object' && !res.message) {
                return res
            }
            return Promise.reject(res)
        }
    },
    (error) => {

        console.log('err' + error) // for debug
        if (error.code === 'ECONNABORTED' || error.message === 'Network Error' || error.message.includes('timeout')) {
            message.error('网络请求超时')
        }else if(error.response.status === 401){
            token.clear()
            router.push(`${state.loginRoute}`)
            if(error.response.data.code !== 40001){
                message.error(error.response.data.message)
            }
        }else if(error.response.status === 500){
            action.openErrorPage(error.response.data)
        }else {
            let msg
            if (error.response && error.response.data.message) {
                msg = error.response.data.message
            } else {
                msg = error.message
            }
            message.error(msg)
        }
        return Promise.reject(error)
    }
)
export function responseBefore(data){
    if(data.message && data.message.name){
        data.message = h(render,{data:data.message})
    }
    if(data.content && data.content.name){
        data.content = h(render,{data:data.content})
    }
    if(data.description && data.description.name){
        data.description = h(render,{data:data.description})
    }
    data.appContext = app._context
    return data
}
export function responseHandel(data){
    if (data.url) {
        if (data.url == 'back') {
            router.back()
        } else {
            jump(data.url)
        }
    }
    if (data.menu) {
        action.setMenu(data.menu)
    }
    if (data.refresh) {
        action.loadComponent(router.currentRoute.value.fullPath,true)
    }

    if(data.type === 'success'){
        return {
            code:200,
            data:data.data || []
        }
    }else{
        return Promise.reject()
    }
}
export default service
